﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;

namespace CardLogistics.Web.MainPage.Purchase.Contract_PayInfo
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.Purchase.Purchase_Contract_PayinfoData dal = new DAL.Purchase.Purchase_Contract_PayinfoData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                BindDLL(ddlsupName, "basic_suppliers", "suppliersname", "suppliersid", " and STATUS=0 and ISDELETE=0");  //供应商
                BindRpt(1);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = dal.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                        resCount++;
                }
                BindRpt(1);
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(int pageIndex)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_CheckNo.Text.Trim().Length > 0)
                sbStr.Append(" and CheckNo like '%" + txt_CheckNo.Text.Trim().ToUpper() + "%' ");//凭证号
            if (txt_InvoiceNo.Text.Trim().Length > 0)
                sbStr.Append(" and InvoiceNo like '%" + txt_InvoiceNo.Text.Trim().ToUpper() + "%' ");//发票单号
            if (txt_StartPayDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(PayDate,'yyyy-MM-dd')>='" + txt_StartPayDate.Text.Trim() + "'");//付款开始日期
            if (txt_EndPayDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(PayDate,'yyyy-MM-dd')<='" + txt_EndPayDate.Text.Trim() + "'");//付款结束日期
            if (PAYTYPE.SelectedValue !="-1")
                sbStr.Append(" and PAYTYPE ='" + PAYTYPE.SelectedValue + "'");//付款方式
            if (ddlsupName.SelectedIndex != 0)
                sbStr.Append(" and SuppId='" + ddlsupName.Value + "'");//供应商
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_purchase_contract_payinfo", "ID", " CheckNo desc,PayDate desc", AspNetPager1.PageSize, pageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);
            #region 导出数据
            //导出语句
            string strExpSql = "select CheckNo 凭证号,ConNo 合同编号,SuppliersName 供应商名称,INVOICENO 发票号,PAYMONEY 付款金额,PAYDATE 付款日期,PayTypeName 付款方式,ATTN 经办人,REMARK 备注 from  v_purchase_contract_payinfo where" + sbStr.ToString();
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt(AspNetPager1.CurrentPageIndex);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            BindRpt(1);
        }
    }
}